package fina2.dcs.auth;

import java.io.IOException;
import java.util.Map;

import javax.security.auth.Subject;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.LoginContext;

import org.apache.log4j.Logger;
import org.jboss.security.auth.spi.LdapExtLoginModule;

public class FinaDcsLoginModuleLDAP extends LdapExtLoginModule {

	private Logger log = Logger.getLogger(getClass());
	private Subject subject = null;
	private CallbackHandler ch = null;

	private boolean loginSuccess = false;
	private boolean commitSuccess = false;

	public void initialize(Subject subject, CallbackHandler ch, Map sharedState, Map options) {
		log.info("Initializing LDAP Login Module");
		this.subject = subject;
		this.ch = ch;
	}

	public boolean login() {
		log.info("Trying to log in");
		try {

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return loginSuccess;
	}

	public boolean commit() {
		log.info("Commiting");
		try {

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return commitSuccess;
	}

	public boolean abort() {
		log.info("Aborting...");
		return false;
	}

	public boolean logout() {
		log.info("Log out");
		return false;
	}
	// protected void doLogin(String user, String pass) {
	// try {
	// log.info("Doing Login ");
	// CallbackHandler handler = new UserPassHandler(user, pass);
	// LoginContext lc = new LoginContext("example", handler);
	// lc.login();
	//
	// } catch (Exception e) {
	// log.info("Login failed", e);
	// }
	// return;
	// }
	//
	// class UserPassHandler implements CallbackHandler {
	// private String user, pass;
	//
	// private UserPassHandler(String user, String pass) {
	// super();
	// this.user = user;
	// this.pass = pass;
	// }
	//
	// public void handle(Callback[] callbacks) throws IOException,
	// UnsupportedCallbackException {
	// for (int i = 0; i < callbacks.length; i++) {
	// if (callbacks[i] instanceof NameCallback) {
	// NameCallback nc = (NameCallback) callbacks[i];
	// nc.setName(user);
	// } else if (callbacks[i] instanceof PasswordCallback) {
	// PasswordCallback pc = (PasswordCallback) callbacks[i];
	// pc.setPassword(pass.toCharArray());
	// } else {
	// throw new UnsupportedCallbackException(callbacks[i],
	// "Unrecognized Callback");
	// }
	// }
	// }
	// }
}
